
#ifndef _GOOGLEBUZZFORM_H_
#define _GOOGLEBUZZFORM_H_

#include <FBase.h>
#include <FWeb.h>
#include <FUi.h>
#include <FNet.h>
#include <FText.h>
#include <FSystem.h>

#include "../../Model/CurrentLocationService.h"
#include "../../Utility/Json/JsonReader.h"
#include "../../Ui/WebController.h"
#include "../../Utility/MyUtil.h"
//#include "JsonObject.h"
#include <FIo.h>

using namespace Osp::Base;
using namespace Osp::Ui::Controls;
using namespace Osp::Web::Controls;
using namespace Osp::Net::Http;
using namespace Osp::Base::Collection;
using namespace Osp::System;
using namespace Osp::Graphics;
using namespace Osp::App;
using namespace Osp::Io;

class Googlebuzzform : public Osp::Web::Controls::ILoadingListener,
					   public IHttpTransactionEventListener,
					   public Osp::Ui::Controls::Form,
					   public ICurrentLocationMember
{

// Construction
public:
	Googlebuzzform(void);
	virtual ~Googlebuzzform(void);
	bool Initialize();
	result OnInitializing(void);
	result OnTerminating(void);
	result CreateWebControl(void);
// Implementation
protected:

// Generated call-back functions
public:

private:
	const static int NONE = -1;
	const static int GET_TOKEN = 0;
	const static int REFRESH_TOKEN = 1;
	const static int SEARCH_ACTIVITIES = 2;
	const static int CREATE_ACTIVITY = 3;
	const static int UPDATE_ACTIVITY = 4;
private:
	int status;
	Web *pWeb;
	String code;
	String clientID;
	String clientSecret;
	String redirectUri;
	String apiKey;
	String accessToken;
	String refreshToken;

	double longitude;
	double latitude;

	int idActivity;
	static int FormId;

public:
	void SetFormId(int id){
		FormId = id;
	}

	static int GetFormId(){
		return FormId;
	}
public:
	void GetAccessCode();
	void GetAccessToken();
	/*void SetListener(IGoogleBuzzEventListener * listener){
		this->listener = listener;
	}*/

	void GetAdditionalAccessToken();
	void GetCurrentLocation();

	void CreateNewActivity(String content);
	void ProcessCreateNewActivity(HttpSession &httpSession, HttpTransaction &httpTransaction);

	void UpdateActivity(String content);
	void ProcessUpdateActivity(HttpSession &httpSession, HttpTransaction &httpTransaction);

	void ProcessGetToken(HttpSession &httpSession,HttpTransaction &httpTransaction);

	/*
	 * pBody is null if method = get
	 * pHeader is null if not need
	 */
	void Download(String url, ByteBuffer *pBody, HttpHeader *pHeader, NetHttpMethod method);

public:
	//Loading Listener
	virtual void  OnEstimatedProgress (int progress);
	virtual void  OnHttpAuthenticationCanceled (void);
	virtual bool  OnHttpAuthenticationRequestedN (const Osp::Base::String &host, const Osp::Base::String &realm, const Osp::Web::Controls::AuthenticationChallenge &authentication);
	virtual void  OnLoadingCanceled (void);
	virtual void  OnLoadingCompleted (void);
	virtual void  OnLoadingErrorOccurred (LoadingErrorType error, const Osp::Base::String &reason);
	virtual bool  OnLoadingRequested (const Osp::Base::String &url, WebNavigationType type);
	virtual void  OnLoadingStarted (void);
	virtual void  OnPageTitleReceived (const Osp::Base::String &title);
	virtual DecisionPolicy  OnWebDataReceived (const Osp::Base::String &mime, const Osp::Net::Http::HttpHeader &httpHeader);

	//HttpTransactionEventListener
	virtual void  OnTransactionAborted (HttpSession &httpSession, HttpTransaction &httpTransaction, result r);
	virtual void  OnTransactionCertVerificationRequiredN (HttpSession &httpSession, HttpTransaction &httpTransaction, Osp::Base::String *pCert);
	virtual void  OnTransactionCompleted (HttpSession &httpSession, HttpTransaction &httpTransaction);
	virtual void  OnTransactionHeaderCompleted (HttpSession &httpSession, HttpTransaction &httpTransaction, int headerLen, bool bAuthRequired);
	virtual void  OnTransactionReadyToRead (HttpSession &httpSession, HttpTransaction &httpTransaction, int availableBodyLen);
	virtual void  OnTransactionReadyToWrite (HttpSession &httpSession, HttpTransaction &httpTransaction, int recommendedChunkSize);

	void setLatLon(double latitude, double longitude){
		this->latitude = latitude;
		this->longitude = longitude;
	}

public:
	//location update
	void LocationUpdate(float latitude, float longitude);

	void SearchActivity();
	void ProcessSearchActivity(HttpSession &httpSession, HttpTransaction &httpTransaction);
};

#endif
